<template>
  <transition name="fade">
    <div v-if="loading" class="loading-wrapper" :class="{ dark: isDark }">
      <template v-if="currentType === 'default'">
        <n-spin size="large">
          <template #description>
            <span class="loading-text">{{ text || loadingText }}</span>
          </template>
        </n-spin>
      </template>
      <template v-else-if="currentType === 'dly'">
        <div class="dly-loading">
          <div class="dly-spinner"></div>
          <span class="dly-text">{{ text || loadingText }}</span>
        </div>
      </template>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { storeToRefs } from 'pinia'
import { useLayoutStore } from '@/store/modules/layout'
import { useLoading } from '@/utils/loadingManager'
import { useLoading as useLocalLoading } from './loadingManager'

const layoutStore = useLayoutStore()
const { layout } = storeToRefs(layoutStore)
const { loading, loadingText } = useLoading()
const { currentType } = useLocalLoading()

const isDark = computed(() => layout.value.theme === 'dark')

defineProps<{
  text?: string
  background?: string
  textColor?: string
}>()
</script>

<style scoped>
.loading-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-wrapper.dark {
  background: rgba(0, 0, 0, 0.45);
}

.loading-text {
  margin-top: 8px;
  font-size: 14px;
  color: var(--primary-color);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.dly-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.dly-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--primary-color);
  border-radius: 50%;
  border-top-color: transparent;
  animation: spin 1s linear infinite;
}

.dly-text {
  color: var(--primary-color);
  font-size: 14px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>
